
export default class Kits {


    //obje的属性个数
    public static attributeCount(obj: Object) {
        let count = 0;
        for (let i in obj) {
            count++;
        }
        return count;
    }

    //随机object一个属性值
    public static randomAttribute(obj: Object) {
        let count = this.attributeCount(obj);
        let inter = this.randomInter(0, count - 1);

        let sum = 0;
        for (let key in obj) {
            if (sum == inter) {
                return obj[key];
            }
            sum++;
        }
    }

    //随机整数
    public static randomInter(min, max) {
        return Math.trunc(Math.random() * (max - min + 1)) + min;
    }

    //加载图片精灵帧
    public static loadSpriteFrame(sprite, path) {
        cc.resources.load(path, cc.SpriteFrame, (err, spriteFrame) => {
            if (!err) {
                if (cc.isValid(sprite)) {
                    sprite.spriteFrame = spriteFrame;
                }
            }
        });
    }

    //年龄显示
    public static formatAge(age) {
        let year = Math.floor(age / 12);
        let monty = age % 12;
        let ageStr = "";
        if (year > 0) {
            if (monty > 0) {
                ageStr = `年龄: ${year}年${monty}月`
            } else {
                ageStr = `年龄: ${year}年`
            }
        } else {
            ageStr = `年龄: ${monty}月`
        }
        return ageStr;
    }

}
